<?php
/**
 * 排放量的封装
 */

namespace app\common\service;

use app\common\service\companyIndustry\realtime\NitreEmissionService;
use app\common\service\companyIndustry\realtime\RealtimeEmissionService;
use app\common\service\companyIndustry\realtime\SulfurEmissionService;
use app\common\service\companyIndustry\simulation\SimulationEmissionService;

class EmissionService extends BaseService
{

    // +----------------------------------------------------------------------
    // | （十）获取所有企业 小时排放量
    // +----------------------------------------------------------------------
    /**
     * +----------------------------------------------------------
     * 10.1 所有公司 - 所有排口 - 排放量
     * 获取实时企业 - 排口 - 脱硫小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param string $endDay
     * @param int $daysNumber
     * @param string $emissionUnit
     * +----------------------------------------------------------
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getAllCompanyDrainEmissionMinToHourAvg($endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg')
    {
        try {
            $sulfur = new SulfurEmissionService();
            $nitre = new NitreEmissionService();
            $simulation = new SimulationEmissionService();
            //step1:实时企业
            //脱硫
            $sulfurData = $sulfur->getRealtimeAllCompanyDrainEmissionSulfurMinToHourAvg($endDay, $daysNumber, $emissionUnit);
            //脱硝
            $nitreData = $nitre->getRealtimeAllCompanyDrainEmissionNitreMinToHourAvg($endDay, $daysNumber, $emissionUnit);
            //根据相同的键值合并
            $companyRealtime = [];
            foreach ($sulfurData as $k => $v) {
                foreach ($nitreData as $k2 => $v2) {
                    if ($k == $k2) {
                        $companyRealtime[$k] = array_merge($v, $v2);
                    }
                }
            }

            //step2:模拟企业
            $companySimulation = $simulation->getSimulationAllCompanyDrainEmissionHourAvg($emissionUnit);

            //step3:合并
            $data = array_merge($companyRealtime, $companySimulation);
            if (!$data) {
                exception('获取小时数据失败');
            }
            return $data;
        } catch (\Exception $e) {
            die ($e->getMessage());
        }
    }



    /**
     * +----------------------------------------------------------
     * 10.2 所有公司 - 排放量
     * 获取实时企业 - 排口 - 脱硫小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param string $endDay
     * @param int $daysNumber
     * @param string $emissionUnit
     * +----------------------------------------------------------
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getAllCompanyEmissionMinToHourAvg($endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg')
    {
        try {
            $sulfur = new SulfurEmissionService();
            $nitre = new NitreEmissionService();
            $simulation = new SimulationEmissionService();
            //step1:实时企业
            //脱硫
            $sulfurData = $sulfur->getRealtimeAllCompanyEmissionSulfurMinToHourAvg($endDay, $daysNumber, $emissionUnit);
            //脱硝
            $nitreData = $nitre->getRealtimeAllCompanyEmissionNitreMinToHourAvg($endDay, $daysNumber, $emissionUnit);
            //根据相同的键值合并
            $companyRealtime = [];
            foreach ($sulfurData as $k => $v) {
                foreach ($nitreData as $k2 => $v2) {
                    if ($k == $k2) {
                        $companyRealtime[$k] = array_merge($v, $v2);
                    }
                }
            }

            //step2:模拟企业
            $companySimulation = $simulation->getSimulationAllCompanyEmissionHourAvg($emissionUnit);

            //step3:合并
            $data = array_merge($companyRealtime, $companySimulation);
            if (!$data) {
                exception('获取小时数据失败');
            }
            return $data;
        } catch (\Exception $e) {
            die ($e->getMessage());
        }
    }

    /**
     * +----------------------------------------------------------
     * 10.3 单个公司 - 排放量
     * 获取实时企业 - 排口 - 脱硫小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param int $companyId
     * @param string $endDay
     * @param int $daysNumber
     * @param string $emissionUnit
     * @param bool $everyDay 是否输出每天
     * +----------------------------------------------------------
     * @return mixed
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getSingleCompanyEmissionMinToHourAvg($companyId, $endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg', $everyDay = false)
    {
        try {
            $realtime = new RealtimeEmissionService();
            $simulation = new SimulationEmissionService();

            $newData = [];
            if ((int)$companyId == 1) {  //step1:实时企业
                //脱硫/TSP/脱硝
                $data    = $realtime->getRealtimeSingleCompanyEmissionMinToHourAvg((int)$companyId, $endDay, $daysNumber, $emissionUnit, $everyDay);
                $newData = $data;
            } else {      //step2:模拟企业
                $data = $simulation->getSimulationSingleCompanyEmissionHourAvg((int)$companyId, $emissionUnit, $everyDay);
                //增加日期字段
                if ($everyDay) {
                    if ($endDay == 'yesterday') {
                        $startDay = strtotime('0 day') - 3600 * 24 * $daysNumber;
                    } else {
                        $startDay = strtotime($endDay);
                    }
                    for ($i = 0; $i < $daysNumber; $i++) {
                        $newData[$i]['publish_time'] = date('Y-m-d', $startDay - $i * 3600 * 24);
                        foreach ($data[0] as $k2 => $v2) {
                            $newData[$i][$k2] = $v2;
                        }
                    }
                } else {
                    $newData = $data;
                }
            }
            return $newData;
        } catch (\Exception $e) {
            exception($e->getMessage() . '获取小时数据失败');
        }
    }

    /**
     * +----------------------------------------------------------
     * 10.4 获取单个公司包括排口的排放量
     * +----------------------------------------------------------
     * @param $companyId
     * @param string $endDay
     * @param int $daysNumber
     * @param string $emissionUnit
     * +----------------------------------------------------------
     * @return array|mixed
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-05
     * +----------------------------------------------------------
     */
    public function getSingleCompanyDrainEmissionMinToHourAvg($companyId, $endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg')
    {
        try {
            $realtime = new RealtimeEmissionService();
            $simulation = new SimulationEmissionService();
            if ((int)$companyId == 1) {  //step1:实时企业
                //脱硫/TSP/脱硝
                $data = $realtime->getRealtimeSingleCompanyDrainEmissionMinToHourAvg((int)$companyId, $endDay, $daysNumber, $emissionUnit);
            } else {      //step2:模拟企业
                $data = $simulation->getSimulationSingleCompanyDrainEmissionHourAvg((int)$companyId, $emissionUnit);
            }
            return $data;
        } catch (\Exception $e) {
            exception($e->getMessage() . '获取小时数据失败');
        }
    }

    // +----------------------------------------------------------------------
    // | （十）公司以及排口 小时排放量
    // +----------------------------------------------------------------------

    /**
     * 实时企业公司和排口
     * @param $companyId
     * @param string $endDay
     * @param int $daysNumber
     * @param string $emissionUnit
     * @return mixed
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function getSingleCompanyAndDrainEmissionMinToHourAvg($companyId, $endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg')
    {
        //step1:公司
        $company = $this->getSingleCompanyEmissionMinToHourAvg((int)$companyId, $endDay, $daysNumber, $emissionUnit);

        //step2:排口
        $drainList = $this->getSingleCompanyDrainEmissionMinToHourAvg((int)$companyId, $endDay, $daysNumber, $emissionUnit);

        //step3:组合信息
        //step3-1:基本信息
        $data['id']           = $company['company_id'];
        $data['company_name'] = $company['company_name'];
        $data['start_time']   = $endDay;
        $data['expire_time']  = $daysNumber * 24;
        //step3-2:公司信息
        $data['company']['so2_emissions'] = (int)$companyId == 1 ? $company['so2_emissions_supplement'] : $company['so2_emissions'];
        $data['company']['nox_emissions'] = (int)$companyId == 1 ? $company['nox_emissions_supplement'] : $company['nox_emissions'];
        $data['company']['tsp_emissions'] = (int)$companyId == 1 ? $company['tsp_emissions_supplement'] : $company['tsp_emissions'];
        //step3-3:排口信息
        foreach ($drainList as $k => $v) {
            $data['drain'][$k]['name']          = $v['drain_name'];
            $data['drain'][$k]['so2_emissions'] = (int)$companyId == 1 ? $company['so2_emissions_supplement'] : $company['so2_emissions'];
            $data['drain'][$k]['nox_emissions'] = (int)$companyId == 1 ? $company['nox_emissions_supplement'] : $company['nox_emissions'];
            $data['drain'][$k]['tsp_emissions'] = (int)$companyId == 1 ? $company['tsp_emissions_supplement'] : $company['tsp_emissions'];
        }

        return $data;
    }

    //
    // +----------------------------------------------------------------------
    // | （十）行业排放量 - 公司/排口 - 小时排放量
    // +----------------------------------------------------------------------

    /**
     * +----------------------------------------------------------
     * 10.1 所有公司 - 所有排口 - 排放量
     * 获取实时企业 - 排口 - 脱硫小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param int $industryId 行业ID
     * @param string $endDay
     * @param int $daysNumber
     * @param string $emissionUnit
     * @param string $everyDay
     * +----------------------------------------------------------
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getAllIndustryCompanyDrainEmissionMinToHourAvg($industryId, $endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg')
    {
        try {
            $realtime = new RealtimeEmissionService();
            $simulation = new SimulationEmissionService();
            $companyRealtime = [];
            if ($industryId == 1) {
                $realtime->getRealtimeDrainMinToHourAvg($endDay, $daysNumber, $emissionUnit);
            }

            //step2:模拟企业
            $companySimulation = $simulation->getSimulationAllIndustryCompanyDrainEmissionHourAvg($industryId, $emissionUnit);

            //step3:合并
            $data = array_merge($companyRealtime, $companySimulation);
            if (!$data) {
                exception('获取小时数据失败');
            }
            return $data;
        } catch (\Exception $e) {
            die ($e->getMessage());
        }
    }

    // +----------------------------------------------------------------------
    // | （十一）行业排放量 - 公司 - 天排放量
    // +----------------------------------------------------------------------

    /**
     * 获取单个行业 - 所有企业 - 天数据
     * @param $industryId
     * @param string $endDay
     * @param int $daysNumber
     * @param string $emissionUnit
     * @param bool $everyDay
     * @param bool $supplement 是否实数
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function getSingleIndustryEmissionDayAvg($industryId, $endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 't', $everyDay = false, $supplement = false)
    {
        try {
            $realtime = new RealtimeEmissionService();
            $simulation = new SimulationEmissionService();

            //step1:模拟企业
            $companySimulation = $simulation->getSimulationSingleIndustryEmissionDayAvg($industryId, $emissionUnit);

            //step2:实时企业
            $companyRealtime = [];
            if ($industryId == 1) {
                $companyRealtime = $realtime->getRealtimeAllCompanyEmissionsMinToHourAvg($endDay, $daysNumber, $emissionUnit, $industryId, $everyDay, $supplement);
            }

            //step3:合并
            $data = [];
            if (!empty($companyRealtime)) {
                foreach ($companyRealtime as $k2 => $v2) {
                    foreach ($companySimulation as $k3 => $v3) {
                        if ((int)$v2['industry_id'] == (int)$v3['industry_id']) {
                            $data[$k2] = array_merge($v2, $v3);
                        }
                    }
                }
            } else {
                $data = $companyRealtime;
            }
            if (!$data) {
                exception('获取小时数据失败');
            }
            return $data;
        } catch (\Exception $e) {
            die ($e->getMessage());
        }
    }
}